<template>
  <div class="addplan">
    <el-card shadow="never" class="addplanTop">
      <el-row class="search">
        <el-col :span="10">
          <div class="addtitle">
            <i class="el-icon-receiving"></i>
            <span>添加测试计划</span>
          </div>
        </el-col>
      </el-row>
    </el-card>

    <el-card shadow="hover" class="addplanmain">
      <el-row>
        <el-col :span="13" :offset="3">
          <el-form
            ref="planFrom"
            :model="planFrom"
            :rules="rules"
            class="planFrom"
            label-width="100px"
          >
            <el-form-item label="计划名称" prop="name">
              <el-input v-model="planFrom.name"></el-input>
            </el-form-item>

            <el-form-item label="环境选择">
              <el-select v-model="planFrom.venid" placeholder="请选择">
                <el-option
                  v-for="item in envlsit"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="选择用例">
              <el-button type="primary" @click="selectDlg">选择用例</el-button>
            </el-form-item>

            <el-form-item label="用例列表">
              <el-table
                :data="planFrom.caselist"
                style="width: 100%"
                stripe
                border
              >
                <el-table-column
                  type="index"
                  width="50"
                  label="序号"
                ></el-table-column>
                <el-table-column
                  prop="id"
                  label="用例编号"
                  width="80"
                ></el-table-column>
                <el-table-column
                  prop="name"
                  label="用例名称"
                  width="200"
                  :show-overflow-tooltip="true"
                ></el-table-column>
                <el-table-column
                  prop="apimethod"
                  label="请求类型"
                  width=""
                ></el-table-column>
                <el-table-column
                  prop="api"
                  label="接口路径"
                  width="350"
                  :show-overflow-tooltip="true"
                ></el-table-column>
                <el-table-column label="操作" fixed="right" width="130">
                  <template slot-scope="scope">
                    <el-tooltip
                      class="item"
                      effect="dark"
                      content="移除"
                      placement="top-start"
                    >
                      <el-button
                        type="danger"
                        icon="el-icon-delete"
                        circle
                        size="mini"
                        @click="caseMove(scope.row.id, scope.$index)"
                      >
                      </el-button>
                    </el-tooltip>

                    <el-tooltip
                      class="item"
                      effect="dark"
                      content="上移"
                      placement="top-start"
                    >
                      <el-button
                        type="success"
                        icon="el-icon-caret-top"
                        circle
                        size="mini"
                        @click="setUp(scope.row.id, scope.$index)"
                      >
                      </el-button>
                    </el-tooltip>

                    <el-tooltip
                      class="item"
                      effect="dark"
                      content="下移"
                      placement="top-start"
                    >
                      <el-button
                        type="success"
                        icon="el-icon-caret-bottom"
                        circle
                        size="mini"
                        @click="setDown(scope.row.id, scope.$index)"
                      >
                      </el-button>
                    </el-tooltip>
                  </template>
                </el-table-column>
              </el-table>
            </el-form-item>

            <el-form-item label="执行时间">
              <el-input v-model="planFrom.exctime"></el-input>
            </el-form-item>

            <el-form-item label="企业消息">
              <el-radio-group v-model="planFrom.qymsg">
                <el-radio :label="0">否</el-radio>
                <el-radio :label="1">是</el-radio>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="webhook">
              <el-input v-model="planFrom.webhook"></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="onSubmit">保存</el-button>
              <el-button @click="onCancel">取消</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-card>

    <GetCaseDialog ref="getCase" />
  </div>
</template>

<script>
import GetCaseDialog from "./planChildrem/getCaseDialog.vue";
import { getCenv, addPlan } from "@/network/networks";
import { Loading } from 'element-ui';
export default {
  components: {
    GetCaseDialog,
  },

  data() {
    return {
      planFrom: {
        name: "",
        venid: "",
        caselist: [],
        exctime: "",
        qymsg: 0,
        webhook: "",
      },
      envlsit: [],
      rules: {
        name: [
          { required: true, message: "请输入环境名称", trigger: "blur" },
          {
            min: 2,
            max: 50,
            message: "长度在 2 到 50 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },

  mounted() {
    this.getEnvlist();
  },

  methods: {
    selectDlg() {
      this.$refs.getCase.showDialog = true;
    },

    setCaselist(arr) {
      for (var j = 0; j < arr.length; j++) {
        if (this.judgeisEpty(arr[j])) {
          this.planFrom.caselist.push(arr[j]);
        }
      }
    },

    judgeisEpty(data) {
      for (var i = 0; i < this.planFrom.caselist.length; i++) {
        if (this.planFrom.caselist[i].id == data.id) {
          return false;
        }
      }
      return true;
    },

    getEnvlist() {
      getCenv()
        .then((res) => {
          if (res.code == 1) {
            this.envlsit = res.data;
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },

    onSubmit() {
      this.$refs["planFrom"].validate((valid) => {
        if (!valid) {
          return;
        }
        let loadingInstance = Loading.service({
            text: '请稍等',
            target: document.querySelector('.addplan')
        });
        addPlan(this.planFrom)
          .then((res) => {
            if (res.code == 1) {
              this.$message.success(res.msg);
              loadingInstance.close();
              this.$router.push("/planmanage");
            } else {
              this.$message.error(res.msg);
            }
          })
          .catch((err) => {
            this.$message.error(err);
          });
          loadingInstance.close();
      });
    },

    onCancel() {
      this.$router.push("/planmanage");
    },

    caseMove(id, index) {
      if (this.planFrom.caselist[index].id == id) {
        this.planFrom.caselist.splice(index, 1);
      } else {
        this.$message.error("数据问题:id与index不匹配，移除失败");
      }
    },
    setUp(id, index) {
      if (index == 0) {
        this.$message.warning("第一条不能上移了");
      } else if (this.planFrom.caselist[index].id == id) {
        var data = this.planFrom.caselist[index];
        this.planFrom.caselist[index] = this.planFrom.caselist[index - 1];
        this.planFrom.caselist[index - 1] = data;
        this.planFrom.caselist.splice(1, 0);
        this.$message.success("用例上移成功");
      } else {
        this.$message.error("数据问题:id与index不匹配，上移失败");
      }
    },
    setDown(id, index) {
      if (index + 1 == this.planFrom.caselist.length) {
        this.$message.warning("最后一条不能下移了");
      } else if (this.planFrom.caselist[index].id == id) {
        var data = this.planFrom.caselist[index];
        this.planFrom.caselist[index] = this.planFrom.caselist[index + 1];
        this.planFrom.caselist[index + 1] = data;
        this.planFrom.caselist.splice(1, 0);
        this.$message.success("用例下移成功");
      } else {
        this.$message.error("数据问题:id与index不匹配，下移失败");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.addtitle {
  font-size: 28px;
  color: #409eff;
  span {
    padding-left: 10px;
  }
}
.addplanmain {
  margin-top: 10px;
  padding: 30px 0;
}
</style>